package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.baseservice.impl.BaseServiceImpl;
import com.qf.entity.User;
import com.qf.mapper.IUserMapper;
import com.qf.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @author 刘亚康
 * @Date 2020/3/26
 * @Version 1.0'
 * @Description
 */
@Slf4j
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private IUserMapper userMapper;

    @Override
    protected BaseMapper<User> getBaseMapper() {
        return userMapper;
    }


    /**
     * 查询总条数
     * @return
     */
    public long getAllTotal(){
        long total=userMapper.selectCount(null);
        System.out.println("查询的总条数为："+total);
        return total;
    }

    /**
     * 根据分页信息查询用户信息
     *
     * @param page
     */
    @Override
    public Page<User> getUserByPage(Page<User> page) {
        log.info("开始分页查询");
        long current=page.getCurrent();
        long size=page.getSize();
        long start=(current-1)*size;
        userMapper.selectPage(page,null);
        List<User> userList=userMapper.getUserByPage(start,size);
        page.setRecords(userList);
        return page;
    }

    /**
     * 查询用户名是否唯一
     *
     * @param param
     * @return
     */
    @Override
    public User checkUsernameIsOne(String param) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",param);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 查询邮箱是否唯一
     *
     * @param param
     * @return
     */
    @Override
    public User CheckEmail(String param) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",param);
        return userMapper.selectOne(wrapper);
    }
}
